package com.opal.stockTaking.service.Impl;

import com.opal.system.entity.table.OpalUser;
import com.opal.system.entity.table.PKOrg;
import com.opal.stockTaking.entity.BdStordoc;
import com.opal.stockTaking.entity.StockTakingResult;
import com.opal.stockTaking.form.StockTakignResultForm;
import com.opal.stockTaking.mapper.oracle.StockTakingHelpMapper;
import com.opal.stockTaking.mapper.mysql.StockTakingResultMapper;
import com.opal.stockTaking.service.StockTakingHelpService;
import com.opal.utils.GetNullPropertyNames;
import com.opal.utils.PageVOUtils;
import com.opal.utils.ResultMsg;
import com.opal.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StockTakingHelpServiceImpl implements StockTakingHelpService {

    @Autowired
    private StockTakingHelpMapper stockTakingHelpMapper;

    @Autowired
    private StockTakingResultMapper stockTakingResultMapper;

    public OpalUser getUser() {
        Subject subject = SecurityUtils.getSubject();
        return (OpalUser) subject.getPrincipal();

    }


    @Override
    public List<PKOrg> getpkorg() {
        return stockTakingHelpMapper.getpkorg();
    }

    @Override
    public List<BdStordoc> getBdStordoc() {
        return stockTakingHelpMapper.getBdStordoc();
    }

    @Override
    public String getmaterialName(String materialCode) {
        return stockTakingHelpMapper.getmaterialName(materialCode);
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ResultMsg deleteByPrimaryKey(String vbillcode) {
        OpalUser user = getUser();
        if (user == null) {
            return new ResultMsg(404, null);
        }
        try {
            stockTakingResultMapper.deleteByPrimaryKey(vbillcode);
            log.info("用户:"+user.getUserName()+"执行了删除票号为:"+vbillcode+" 的记录信息!");
            return new ResultMsg(200,"删除票号:"+vbillcode+" 的记录信息!");
        }catch (Exception e){
            return new ResultMsg(500,"删除票号信息失败!");
        }
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ResultMsg insertSelective(String date, int startCode, int count) {

        if (getUser() == null) {
            return new ResultMsg(404, null);
        }
        List<StockTakingResult> stockTakingResultList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            StockTakingResult stockTakingResult = new StockTakingResult();
            stockTakingResult.setVbillcode("No." + startCode++);
            stockTakingResult.setVbilldate(date);
            stockTakingResultList.add(stockTakingResult);
        }
        try {
            stockTakingResultMapper.insertSelective(stockTakingResultList);
            return new ResultMsg(200, "增加了" + count + "条盘点票号!");
        } catch (Exception e) {
            return new ResultMsg(500, "增加的数据中有重复的盘点票号!");
        }
    }

    @Override
    public List<StockTakingResult> selectSelective(StockTakingResult stockTakingResult) {
        String vbillcodes = null;
        if (stockTakingResult.getVbillcode()!= "" && stockTakingResult.getVbillcode() != null) {
            stockTakingResult.setVbillcode(stockTakingResult.getVbillcode() + "%");
        }
        return stockTakingResultMapper.selectSelective(stockTakingResult);
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public ResultMsg updateByPrimaryKeySelective(StockTakingResult stockTakingResult) {
        if (getUser() == null) {
            return new ResultMsg(404, null);
        }
        int ac;
        try {
            ac = stockTakingResultMapper.updateByPrimaryKeySelective(stockTakingResult);
        }catch (Exception e){
            return new ResultMsg(500,"修改失败!");
        }
        if (ac == 0){
            return new ResultMsg(500,"修改失败!");
        }else {
            return new ResultMsg(200,"修改成功!");
        }
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public PageVO<StockTakingResult> stockTakingResult(StockTakignResultForm form, List<String> bdStordoc) {

        StockTakingResult r = new StockTakingResult();
        r.setVbilldate(form.getStockTakingTime());
        r.setCmaterialcode("200");

        List<StockTakingResult> stockTakingResultList;
        List<StockTakingResult> resultOne;
        List<StockTakingResult> resultTwo;
        try {
            //手输
            stockTakingResultList = stockTakingResultMapper.selectSelective(r);

            //sql1
            resultOne = stockTakingHelpMapper.selectOne(form.getPKOrgHelp(), bdStordoc);

            //sql2
            resultTwo = stockTakingHelpMapper.selectTwo(form.getPKOrgHelp(), bdStordoc);
        }catch (Exception e){
            return PageVOUtils.success(null,500,"查询数据表生成失败!");
        }
        List<StockTakingResult> endValue = new ArrayList<>();

        for (StockTakingResult result : stockTakingResultList) {
            int materialcode = stockTakingHelpMapper.getmaterialclass(result.getCmaterialcode());
            if (materialcode == 1 || materialcode == 3) {
                for (StockTakingResult sqlTwo : resultTwo) {
                    if (sqlTwo.getCmaterialcode().equals(result.getCmaterialcode()) && sqlTwo.getCwarehouseid().equals(result.getCwarehouseid())) {
                        BeanUtils.copyProperties(result, sqlTwo, GetNullPropertyNames.getNullPropertyNames(result));
                        if (result.getNnum()!=null) {
                            sqlTwo.setDifference(result.getNnum() - sqlTwo.getNonhandnum());
                        }
                        endValue.add(sqlTwo);
                        try {
                            stockTakingResultMapper.updateByPrimaryKeySelective(sqlTwo);
                        }catch (Exception e){
                            return PageVOUtils.success(null,500,"盘盈亏表生成失败!");
                        }
                    }
                }
            } else {
                for (StockTakingResult sqlOne : resultOne) {
                    if (sqlOne.getCmaterialcode().equals(result.getCmaterialcode()) &&  sqlOne.getCwarehouseid().equals(result.getCwarehouseid())) {
                        BeanUtils.copyProperties(result, sqlOne, GetNullPropertyNames.getNullPropertyNames(result));
                        if (result.getNnum()!=null&&sqlOne.getNonhandnum()!=null) {
                            sqlOne.setDifference(result.getNnum() - sqlOne.getNonhandnum());
                        }
                        endValue.add(sqlOne);
                        try {
                            stockTakingResultMapper.updateByPrimaryKeySelective(sqlOne);
                        }catch (Exception e){
                            return PageVOUtils.success(null,500,"盘盈亏表生成失败!");
                        }
                    }
                }
            }
            return PageVOUtils.success(endValue,200,"盘盈亏表已生成!");
        }
        return null;
    }
}